<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="home" /><span
              >江苏传智播客教育科技股份有限公司</span
            >
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')"
                      >添加子部门</el-dropdown-item
                    >
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 使用el-tree组件展示主题内容 -->
        <el-tree
          default-expand-all
          :data="list"
          :props="{ children: 'children', label: 'name' }"
        >
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)"
                          >添加子部门</el-dropdown-item
                        >
                        <el-dropdown-item @click.native="hEdd(data.id)"
                          >编辑部门</el-dropdown-item
                        >
                        <el-dropdown-item
                          v-if="data.children.length === 0"
                          @click.native="hDel(data.id)"
                          >删除部门</el-dropdown-item
                        >
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
      <!-- 新增子部门或者编辑部门的弹层 -->
      <el-dialog
        :title="isEdit ? ' 编辑' : '添加'"
        :visible.sync="showDialog"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
      >
        <!-- v-if="showDialog" 解决数据不更新的方法1-->
        <!-- ref="myAddRef" 点击按钮调用子组件的函数 -->
        <!-- 在子元素中利用watch来进行监听调用函数 -->
        <add0rEdit
          v-if="showDialog"
          ref="myAddRef"
          :id="curId"
          :isEdit="isEdit"
          @success="hSuccess"
          @close="showDialog = false"
          :originList="originList"
        />
      </el-dialog>
    </div>
  </div>
</template>
<script>
import add0rEdit from "./deptDialog.vue";
import { getDepartments, delDepartments } from "@/api/departments";
import { tranDataToTree } from "@/utils/index";
export default {
  components: {
    add0rEdit,
  },
  data() {
    return {
      // 依赖一份树形数据
      list: [],
      // 控制弹层的显示或隐藏
      showDialog: false,
      // 点击部门的id
      curId: "",
      // false 新增 || true 编辑
      isEdit: false,
      originList: [],
    };
  },
  created() {
    this.loadDepartments();
  },
  methods: {
    // @close="hClose"
//     hClose(){
//  this.$refs.myAddRef.resetForm()
//     },
    // 实现删除部门
    hDel(id) {
      this.$confirm("您确认删除此部门吗??")
        .then(async () => {
          const res = await delDepartments(id);
          if (res.code !== 10000) return this.$message.error(res.message);
          this.$message.success(res.message);
          this.loadDepartments();
        })
        .catch((error) => console.log(error));
    },
    // 重新渲染页面
    hSuccess() {
      // 重新渲染页面
      this.loadDepartments();
      // 关闭弹层
      this.showDialog = false;
    },
    // 加载部门列表
    async loadDepartments() {
      try {
        const res = await getDepartments();
        console.log("getDepartments", res);
        this.originList = res.data.depts.map((item) => {
          return {
            id: item.id,
            pid: item.pid,
            code: item.code,
            name: item.name,
          };
        });
        res.data.depts.shift(); //删除第一天数据 公司名称
        // tranDataToTree(res.data.depts)
        this.list = tranDataToTree(res.data.depts);
      } catch (error) {
        console.log(error);
      }
    },
    // 传入id
    // 添加按钮
    hAdd(id) {
      this.curId = id;
      this.isEdit = false; // 添加状态
      this.showDialog = true;
    },
    // 编辑按钮
    hEdd(id) {
      this.curId = id;
      this.isEdit = true; // 编辑状态
      // this.$nextTick(() => {
      //   this.$refs.myAddRef.loadDepEmployees();
      // });
      this.showDialog = true;
    },
  },
};
</script>
